package com.health.system.controller;

import com.health.system.annotation.Log;
import com.health.system.common.result.Result;
import com.health.system.common.validated.groups.InsertGroup;
import com.health.system.common.validated.groups.UpdateGroup;
import com.health.system.dto.ResourceTreeListDTO;
import com.health.system.service.ResourceService;
import com.health.system.vo.ResourceTreeListSearchVo;
import com.health.system.vo.ResourceVo;
import com.health.system.vo.UpdateStatusVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

import static com.health.system.constant.LogTypeConstant.*;


@Validated
@Api(tags = "接口管理")
@RestController
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    @Log(SELECT)
    @ApiOperation(value = "树形结构资源列表")
    @GetMapping("/admin/resources/treeList")
    public Result<List<ResourceTreeListDTO>> treeList(ResourceTreeListSearchVo vo) {
        List<ResourceTreeListDTO> result = resourceService.treeList(vo);
        return Result.ok(result);
    }

    @Log(INSERT)
    @Validated(InsertGroup.class)
    @ApiOperation(value = "新增资源")
    @PostMapping("/admin/resources/insert")
    public Result<String> insert(@Valid @RequestBody ResourceVo vo) {
        resourceService.insert(vo);
        return Result.ok();
    }

    @Log(UPDATE)
    @Validated(UpdateGroup.class)
    @ApiOperation(value = "更新资源")
    @PostMapping("/admin/resources/update")
    public Result<String> update(@Valid @RequestBody ResourceVo vo) {
        resourceService.update(vo);
        return Result.ok();
    }

    @Log(DELETE)
    @ApiImplicitParam(name = "id", value = "资源id", required = true, dataTypeClass = Long.class)
    @ApiOperation(value = "删除资源")
    @DeleteMapping("/admin/resources/delete")
    public Result<String> deleteById(@NotNull(message = "资源id不能为空") @RequestBody Long id) {
        resourceService.deleteById(id);
        return Result.ok();
    }

    @Log(UPDATE)
    @ApiOperation(value = "修改匿名访问状态")
    @PostMapping("/admin/resources/update/status")
    public Result<String> updateAnonStatus(@Valid @RequestBody UpdateStatusVo vo) {
        resourceService.updateStatus(vo);
        return Result.ok();
    }

}
